#define  _CRT_SECURE_NO_WARNINGS
#include <string>
#include <iostream>
using namespace std;
 
class Solution {
public:
    string removeKdigits(string num, int k) {
        if (num.size() <= k) return "0";
        string stack;
        for (auto e : num)
        {
            if (stack.empty() || e >= *(stack.end() - 1))
                stack += e;
            else
            {
                while (*(stack.end() - 1) > e && k > 0)
                {
                    stack.pop_back();
                    k--;
                }
                stack += e;
            }
        }
        while (k--)
            stack.pop_back();
        int pos = 0;
        while (stack[pos] == '0') pos++;
        if (pos == stack.size()) return "0";
        return stack.substr(pos, num.size() - k);
        //return to_string(stoi(stack.substr(0, num.size() - k)));
    }
};

int main()
{
    string str = "12345678";
    Solution().removeKdigits("1432219", 3);
    std::cout << *(str.end() - 1) << std::endl;
}